package com.example1.hello;

import android.app.Service; 
import android.content.ClipData;
import android.content.ClipData.Item;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.ClipboardManager.OnPrimaryClipChangedListener;
import android.content.Context; 
import android.content.Intent; 
import android.os.Handler;
import android.os.IBinder; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnTouchListener; 
import android.view.WindowManager; 
import android.widget.Button; 
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
 
public class Translator_Service extends Service
{
	static View floatDialogView; 
    WindowManager wm; 
	WindowManager.LayoutParams params; 
	float startX, startY, x, y; 
	int orgtype=0;
	EditText _edittxt1;
	EditText _edittxt2;
	String _nowLan ;
	String _toLan ;
	
	ClipboardManager _clipboard;
	private Handler _TimerHandler = new Handler();  
	boolean _runTimer=false;
	String _preData="";
	Translator _Translator;
	
	
	
	public void onStart(Intent intent, int startId) 
	{ 
_clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

		
		_Translator=new Translator(this);
		_nowLan="en";
		_toLan="zh-tw";
		_clipboard.addPrimaryClipChangedListener(new OnPrimaryClipChangedListener() {  
		              
			            @Override  
			            public void onPrimaryClipChanged() {  
			            	_TimerHandler.post(TimerRunnable);
			            }  
		}); 


		wm = (WindowManager)getApplicationContext().
				 getSystemService(Context.WINDOW_SERVICE); 
				 params = new WindowManager.LayoutParams(); 
				 params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; 
				 orgtype = params.flags;
				 
				 params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; 
				 //params.width = WindowManager.LayoutParams.WRAP_CONTENT; 
				 params.height = WindowManager.LayoutParams.WRAP_CONTENT; 
				 LayoutInflater mInflater = LayoutInflater.from(this); 
				 floatDialogView = mInflater.inflate(R.layout.activity_trans, null); 
				 
				 _edittxt1 = (EditText)floatDialogView.findViewById(R.id.EditText01); 
				 _edittxt2 = (EditText)floatDialogView.findViewById(R.id.editText1);
				 
				 Button btn_close = (Button)floatDialogView.findViewById(R.id.btn_close); 
						  btn_close.setOnClickListener(new OnClickListener() { 
						  @Override 
						  public void onClick(View arg0) { 
							  //wm.removeView(floatDialogView); 
							  new Thread(new Runnable() {
			         	            
			         	            public void run() 
			         	            {
			         	            	
			         	            	_Translator.speak(_edittxt2.getText().toString(), _toLan);
			         	            	
			         	            	
			         	            	
										  
			         	            }
			         	        }).start();
						  } 
						  });
						  
							 Button exit = (Button)floatDialogView.findViewById(R.id.exit); 
							 exit.setOnClickListener(new OnClickListener() { 
							  @Override 
							  public void onClick(View arg0) { 
								  wm.removeView(floatDialogView); 
								 
							  } 
							  });						  
						  
			    Button Button01 = (Button)floatDialogView.findViewById(R.id.Button01);
			    Button01.setOnClickListener(new OnClickListener() { 
					  @Override 
					  public void onClick(View arg0) {
						  
						  new Thread(new Runnable() {
		         	            
		         	            public void run() 
		         	            {
		         	            	
		         	            	_Translator.speak(_edittxt1.getText().toString(), _nowLan);
		         	            	
		         	            	
		         	            	
									  
		         	            }
		         	        }).start();
						  
						  
						
						
					  } 
					  });	
			    
			    Button Button02 = (Button)floatDialogView.findViewById(R.id.Button02); 
			   
			    Button02.setOnClickListener(new OnClickListener() { 
							  @Override 
							    public void onClick(View arg0) 
							    {
								  if(_nowLan.equals("en"))
								  {
									  _toLan = "en";
									  _nowLan="zh-tw";
								   }else
										  {
										  _toLan = "zh-tw";
										  _nowLan="en";
										  }
								  ((Button)View).setText(_nowLan);
							     } 
							  
							     
							  });		  
						  
						  
				Button changeBut = (Button)floatDialogView.findViewById(R.id.change); 
				changeBut.setOnClickListener(new OnClickListener() { 
							  @Override 
							  public void onClick(View arg0) {
								  
								  new Thread(new Runnable() {
				         	            
				         	            public void run() 
				         	            {
				         	            	
				         	            	
				         	            	final String value=  _Translator.Trans(_edittxt1.getText().toString(), _nowLan,_toLan);
				         	            	
				         	            	_TimerHandler.post(new Runnable() {    
				         	            		public void run() {
				         	            			
				         	            			_edittxt2.setText(value);
				         	            		}
				         	            		});
				         	            	
				         	            	
											  
				         	            }
				         	        }).start();
								  
								  
								
								
							  } 
							  });		  
						  
						  
						  
						  
				 
				 
				_edittxt1.setOnClickListener(fouceView);
				_edittxt2.setOnClickListener(fouceView);
				 
				 TextView transwindow = (TextView)floatDialogView.findViewById(R.id.transwindow); 
				 
				 transwindow.setOnTouchListener(new OnTouchListener() { 
					  @Override 
					  public boolean onTouch(View view, MotionEvent event) { 
					  
						  //x = event.getRawX(); 
						  y = event.getRawY(); 
						  switch(event.getAction()) 
						  { 
						  	case MotionEvent.ACTION_DOWN: 
							//  startX = event.getRawX(); 
							  startY = event.getRawY(); 
							  break; 
						  case MotionEvent.ACTION_MOVE: 
							  updatePosition(); 
						  break; 
						  case MotionEvent.ACTION_UP: 
							  updatePosition(); 
							  //startX = startY = 0;
							  startY =0;
						  break; 
						  } 
						  return true; 
					  } 
					  });
				 floatDialogView.setOnTouchListener(new OnTouchListener() { 
					  @Override 
					  public boolean onTouch(View view, MotionEvent event) { 
					  
						  if(params.flags == orgtype)
						  {
							  params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; 
							  wm.updateViewLayout(floatDialogView, params);
						  }
						  return true;
					  } 
					  });
				 
				 wm.addView(floatDialogView, params);
				 
	}
	
	 private void updatePosition(){ 
		// params.x = (int)( x - startX); 
		 params.y = (int) (y - startY); 
		 wm.updateViewLayout(floatDialogView, params); 
	 }
	private Runnable TimerRunnable= new Runnable() {    
        public void run() {  
             
        	boolean isData = _clipboard.hasPrimaryClip();
        	if(isData)
        	{
        		if(_clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))
        		{
	        		ClipData cd = _clipboard.getPrimaryClip();
	        		Item item = cd.getItemAt(0);
	        		String dataString = item.getText().toString();
	        		

	        		
	        		if(dataString.length()>0)
	        		{  	            	
		        		ClipData data = ClipData.newPlainText("", "");	        		
		        		_clipboard.setPrimaryClip(data);
	        			_preData = dataString;
	        			if(!_runTimer)
	        			{
	        				
	        				_edittxt1.setText(_preData);
	        				//_runTimer=true;
	        				
	        				
		        			// new Thread(new Runnable() {
		         	            
		         	        //    public void run() 
		         	        //    {
		         	            	
		         	        //    	_Translator.speak(_preData, "zh-tw");	         	            	
		         	        //    	_runTimer=false; 	
		         	        //    }
		         	        //}).start();
	        			}        			
	        			
	        		}
	        		
	        	}
        		
        	}
            
           

        }  
    };  
    
    private OnClickListener fouceView = new OnClickListener() { 
		  @Override 
		  public void onClick(View arg0) { 
			  if(params.flags != orgtype)
			  {
				  params.flags = orgtype; 
				  wm.updateViewLayout(floatDialogView, params);
			  }
		  } 
    };
    
	
	@Override 
	 public IBinder onBind(Intent arg0) { 
	    return null; 
	 } 
	
	@Override 
	 public void onDestroy() { 
		WindowManager wm = (WindowManager)getApplicationContext(). 
		getSystemService(Context.WINDOW_SERVICE); 
		if (floatDialogView != null) { 
			wm.removeView(floatDialogView); 
		} 
		_runTimer=false;
		
		_Translator.release();
	 }
	
}
